﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace DemoTG
{
    public class NDSHelper
    {

        /// <summary>
        /// 处理超长显示文本
        /// </summary>
        /// <param name="text">将要显示的文本</param>
        /// <returns></returns>
        public static string FormatShownModelName(string text, int maxLength)
        {
            if (string.IsNullOrEmpty(text))
                return text;

            int length = text.Length;
            if (length > maxLength)
            {
                // 显示结果后面的字符为...
                string subString = text.Substring(0, maxLength - 3);
                subString += "...";

                return subString;
            }

            return text;
        }
        /// <summary>
        /// 获取系统临时目录的路径
        /// </summary>
        /// <returns></returns>
        public static string GetSystemTempFolder()
        {
            string tempFolder = System.Environment.GetEnvironmentVariable("TEMP");
            DirectoryInfo dirInfo = new DirectoryInfo(tempFolder);

            return dirInfo.FullName;
        }


        /// <summary>
        /// 判断文件是否只读
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool IsFileReadonly(string filePath)
        {
            if (!File.Exists(filePath))
                return false;

            return (File.GetAttributes(filePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
        }

        /// <summary>
        /// 设置文件的只读
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="bReadOnly"></param>
        /// <returns></returns>
        public static bool SetFileReadOnly(string filePath, bool bReadOnly)
        {
            if (!File.Exists(filePath))
                return false;

            File.SetAttributes(filePath, bReadOnly ? FileAttributes.ReadOnly : FileAttributes.Normal);

            return true;
        }


        /// <summary>
        ///  计算指定文件的MD5值
        /// </summary>
        /// <param name="fileName">指定文件的完全路径名称</param>
        /// <returns>返回值的字符串形式</returns>
        public static String ComputeMD5(String fileName)
        {
            var hashMD5 = String.Empty;
            try
            {
                //检查文件是否存在，如果文件存在则进行计算，否则返回空值            
                if (File.Exists(fileName))
                {
                    string tempFolder = NDSHelper.GetSystemTempFolder();
                    string fName = Path.GetFileName(fileName);
                    string copyfilePath = tempFolder + "\\" + fName;

                    File.Copy(fileName, copyfilePath, true);
                    if (File.Exists(copyfilePath))
                    {
                        SetFileReadOnly(copyfilePath, false);
                        using (var fs = File.Open(copyfilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            //计算文件的MD5值
                            var calculator = MD5.Create();
                            var buffer = calculator.ComputeHash(fs);
                            calculator.Clear();
                            //将字节数组转换成十六进制的字符串形式
                            var stringBuilder = new StringBuilder();
                            for (var i = 0; i < buffer.Length; i++)
                            {
                                stringBuilder.Append(buffer[i].ToString("x2"));
                            }
                            hashMD5 = stringBuilder.ToString();
                        }

                        File.Delete(copyfilePath);
                    }

                }
            }
            catch
            {

            }          

            return hashMD5;
        }

        /// <summary>
        /// 校正装配体中零部件的名字
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string CalibrateOccorName(string name)
        {
            if (string.IsNullOrEmpty(name))
                return name;

            if (name.Contains(":"))
            {
                int index = name.LastIndexOf(':');
                return name.Substring(0, index);
            }

            return name;
        }
    }
}
